package com.nowcoder.community.controller;

import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Event;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.RedisKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.awt.print.Printable;
import java.util.Date;

/**
 * @BelongsProject: community
 * @BelongsPackage: com.nowcoder.community.controller
 * @Author: Master_XXJS
 * @CreateTime: 2023-01-01  14:25
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Controller
@RequestMapping("/comment")
public class CommentController implements CommunityConstant {
    @Autowired
    private CommentService commentService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private EventProducer eventProducer;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加评论
     * @param discussionPostId 帖子的id
     * @param comment 评论的实体对象
     * @return
     */
    @RequestMapping(path = "/add/{discussPostId}",method = RequestMethod.POST)
    public String addComment(@PathVariable("discussPostId") int discussionPostId, Comment comment){
        log.info("回复帖子");
        if (comment == null){
            System.err.println("comment 为空");
        }
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        if (comment.getTargetId() == null){
            comment.setTargetId(0);
        }
        commentService.addComment(comment);

        // 触发评论事件，需要评论的帖子的id，方便进行直接跳转到被评论的帖子
        Event event = new Event()
                .setTopic(TOPIC_COMMENT)
                .setEntityId(comment.getEntityId())
                .setEntityType(comment.getEntityType())
                .setUserId(comment.getUserId())
                //帖子的id，事件event对象没有特定的属性进行存储
                .setData("postId",discussionPostId);
        //设置 收到消息的用户id
        //因为这里的评论可能是  评论帖子，可能是回复
        if(comment.getEntityType() == ENTITY_TYPE_POST){
            //帖子的评论
            DiscussPost target = discussPostService.selectByDiscussPostId(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        } else if (comment.getEntityType() == ENTITY_TYPE_COMMENT) {
            //回复，评论的评论
            Comment target = commentService.getCommentById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }
        eventProducer.fireEvent(event);

        if (comment.getEntityType() == ENTITY_TYPE_POST){
            //触发发帖事件，因为评论的是帖子。帖子需要进行更新
            //触发发布帖子事件
            event = new Event()
                    .setTopic(TOPIC_PUBLISH)
                    .setUserId(comment.getUserId())
                    .setEntityType(ENTITY_TYPE_POST)
                    .setEntityId(discussionPostId);
            eventProducer.fireEvent(event);

            //计算帖子的分数
            String redisKey = RedisKeyUtil.getPostKey();
            redisTemplate.opsForSet().add(redisKey,discussionPostId);
        }

        return "redirect:/discuss/detail/" + discussionPostId;
    }
}

